<?php
/**
 * Created by PhpStorm.
 * User: yzapple
 * Date: 2018/1/25
 * Time: 下午4:20
 */

namespace app\admin\controller;


use app\common\model\Base;
use think\Db;
use think\Request;

class Log extends Basecontroller
{
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('adminlog');

    }

    public function index()
    {

        //搜索
        $request = Request::instance();
        $pageQuery = [];
        $option = [];

        $option['admin_log.is_deleted'] = ['=', 1];
        //默认年月日
        $pageQuery['query']['start'] = date("Y-m-d");
        $join = '';

        if ($request->isGet()) {
            $data = $request->param();

            if (isset($data['admin_type']) && trim($data['admin_type'])) {

                $option['admin_type'] = trim($data['admin_type']);
                $pageQuery['query']['admin_type'] = trim($data['admin_type']);

            }

            if (isset($data['admin_name'])  && trim($data['admin_name']) ) {

                if(isset($data['admin_type'])){
                    switch($data['admin_type']){
                        case 1:
                            $option['admin.admin_name'] = array('like', '%' . trim($data['admin_name']) . '%');

                            $join = "admin.admin_id=admin_log.admin_id";
                            $join_table = "admin";
                            break;
                        case 2:
                            $option['doctor.name'] = array('like', '%' . trim($data['admin_name']) . '%');
                            $join = "doctor.id=admin_log.admin_id";
                            $join_table = "doctor";
                            break;
                    }
                }else{
                    $option['admin.admin_name'] = array('like', '%' . trim($data['admin_name']) . '%');

                    $join = "admin.admin_id=admin_log.admin_id";
                    $join_table = "admin";
                }


                $pageQuery['query']['admin_name'] = trim($data['admin_name']);

            }

            if (isset($data['start']) && $data['start'] !== '') {
                $d = $data['start'];
                $data['end'] = date("Y-m-d", strtotime("$d   +1   day"));
                $option['admin_log.create_time'] = array('between', ["{$data['start']}", "{$data['end']}"]);
                $pageQuery['query']['start'] = trim($data['start']);

            }


            $this->assign('search', isset($pageQuery['query']) ? $pageQuery['query'] : '');
            if($join){
                //print_r($join);
                $list = $this->model
                    ->join($join_table,$join)
                    ->where($option)
                    ->order('admin_log.create_time desc')
                  ->paginate(false, false, $pageQuery)
                 ->each(function ($item, $key) {
                    if($item['admin_type'] == 1){
                        $admin_name = DB::name('admin')->field('admin_name')->where("admin_id={$item['admin_id']}")->find();
                        $item['admin_name'] = $admin_name['admin_name'].'管理员';
                    }else{
                        $admin_name = DB::name('doctor')->field('name')->where("id={$item['admin_id']}")->find();
                        $item['admin_name'] = $admin_name['name'].'医生';
                    }
                    return $item;
                });
               // print_r(DB::name('admin_log')->getLastSql());

            }else{
                $list = $this->model
                    ->where($option)
                    ->order('admin_log.create_time desc')
                    ->paginate(false, false, $pageQuery)
                    ->each(function ($item, $key) {
                        if($item['admin_type'] == 1){
                            $admin_name = DB::name('admin')->field('admin_name')->where("admin_id={$item['admin_id']}")->find();
                            $item['admin_name'] = $admin_name['admin_name'].'管理员';
                        }else{
                            $admin_name = DB::name('doctor')->field('name')->where("id={$item['admin_id']}")->find();
                            $item['admin_name'] = $admin_name['name'].'医生';
                        }

                        return $item;
                    });

              //  print_r(DB::name('admin_log')->getLastSql());
              //  print_r($list);
            }

          //  exit;

          /*  $admin_list = $this->admin_list();
            $this->assign('admin_list', $admin_list);*/

            $this->assign('list', $list);
            $this->assign('url', $this->request->action());

            return $this->fetch();

        }
    }

    /**
     * 删除日志
     */
    public function del_log()
    {

    }
}